<?php

namespace Admin\Controller;


class RecycleController extends CommonController
{

    public function __construct()
    {
        parent::__construct();
        $categories = $this->all_categories();

        $this->assign('categories', $categories);
    }

    /**
     * 商城回收站首页---查出所有分类
     */
    public function all_categories()
    {

        $categories = $this->category->where("parent_id = 0")->select();
        foreach ($categories as &$category) {
            $category['children'] = $this->category->where("parent_id = $category[id]")->select();
        }
        return $categories;
    }

    /**
     * 商品回收站--首页
     */
    public function index()
    {
        //分页
        $count = $this->product->where('status=0')->count();// 查询满足要求的总记录数
        $Page = new \Think\Page($count, 5);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show = $Page->show();// 分页显示输出

        $products = $this->product->where("status = 0")->order("sort_order desc")->select();
        foreach ($products as &$product) {
            $product['category'] = $this->category->where("id = '$product[category_id]'")->find();
        }

        $this->assign('products', $products);
        $this->assign('page',$show);
        $this->assign('count',$count);
        $this->display();
    }

    /**
     * 商品回收站--单个还原
     */
    public function ajax_reply()
    {
        if (IS_AJAX) {
            $id = I('post.id');
            $this->product->where("id = '$id'")->setField('status', 1);
        }
    }

    /**
     * 商品回收站--多个还原
     */
    public function ajax_reply_all()
    {
        if (IS_AJAX) {
            $ids = I('post.ids');
            foreach ($ids as $id) {
                $this->product->where("id = '$id'")->setField('status', 1);
            }
        }
    }

    /**
     * 商品回收站--单个删除
     */
    public function ajax_delete()
    {
        if (IS_AJAX) {
            $id = I('post.id');
            $this->product->where("id = '$id'")->delete();
        }
    }

    /**
     * 商品回收站--多个删除
     */
    public function ajax_delete_all()
    {
        if (IS_AJAX) {
            $ids = I('post.ids');
            foreach ($ids as $id) {
                $this->product->where("id = '$id'")->delete();
            }
        }
    }

    /***
     * 商品回收站--库存管理
     */
    public function ajax_stock()
    {
        if (IS_AJAX) {
            $id = I('post.id');
            $stock = I('post.stock');
            $this->product->where("id = '$id'")->setField('stock', $stock);
        }
    }

}